import { addEvaluation, updateEvaluation, deleteEvaluation, getUserEvaluations, getActivityEvaluations } from '@/api/evaluation'

const state = {
  evaluations: [],
  total: 0,
  loading: false
}

const mutations = {
  SET_EVALUATIONS: (state, evaluations) => {
    state.evaluations = evaluations
  },
  SET_TOTAL: (state, total) => {
    state.total = total
  },
  SET_LOADING: (state, loading) => {
    state.loading = loading
  }
}

const actions = {
  // 获取用户评价列表
  getUserEvaluations({ commit }, params) {
    commit('SET_LOADING', true)
    return new Promise((resolve, reject) => {
      getUserEvaluations(params).then(response => {
        const { data } = response
        commit('SET_EVALUATIONS', data.records || [])
        commit('SET_TOTAL', data.total || 0)
        commit('SET_LOADING', false)
        resolve(data)
      }).catch(error => {
        commit('SET_LOADING', false)
        reject(error)
      })
    })
  },
  
  // 获取活动评价列表
  getActivityEvaluations({ commit }, params) {
    commit('SET_LOADING', true)
    return new Promise((resolve, reject) => {
      getActivityEvaluations(params).then(response => {
        const { data } = response
        commit('SET_EVALUATIONS', data.records || [])
        commit('SET_TOTAL', data.total || 0)
        commit('SET_LOADING', false)
        resolve(data)
      }).catch(error => {
        commit('SET_LOADING', false)
        reject(error)
      })
    })
  },
  
  // 添加评价
  addEvaluation({ dispatch }, evaluationData) {
    return new Promise((resolve, reject) => {
      addEvaluation(evaluationData).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  
  // 更新评价
  updateEvaluation({ dispatch }, evaluationData) {
    return new Promise((resolve, reject) => {
      updateEvaluation(evaluationData).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  
  // 删除评价
  deleteEvaluation({ dispatch }, id) {
    return new Promise((resolve, reject) => {
      deleteEvaluation(id).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} 